package org.rqb.listener;

import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import lombok.extern.slf4j.Slf4j;
import org.rqb.Main;
import org.rqb.frame.MyFrame;
import org.rqb.panel.ClickFrequencyPanel;
import org.rqb.panel.RepeatPanel;

@Slf4j
public class GlobalKeyListener implements NativeKeyListener {

    private MyFrame frame;

    private RepeatPanel repeatPanel;

    private ClickFrequencyPanel clickFrequencyPanel;

    public GlobalKeyListener(MyFrame myFrame) {
        this.frame = myFrame;

    }

    public GlobalKeyListener(MyFrame frame, RepeatPanel repeatPanel,ClickFrequencyPanel clickFrequencyPanel) {
        this.frame = frame;
        this.repeatPanel = repeatPanel;
        this.clickFrequencyPanel = clickFrequencyPanel;
    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {

        if (e.getKeyCode() == NativeKeyEvent.VC_E && NativeKeyEvent.getModifiersText(e.getModifiers()).contains("Ctrl")) { // 检测 Ctrl + E 快捷键

            log.info("快捷键 Ctrl + E 被按下!");
            // 弹出myFrame
            frame.displayAtTheForefront();
            //关闭鼠标监听
            repeatPanel.stopRecordListener();

        }else if (e.getKeyCode() == NativeKeyEvent.VC_Q && NativeKeyEvent.getModifiersText(e.getModifiers()).contains("Ctrl")) { // 检测 Ctrl + Q 快捷键

            log.info("快捷键 Ctrl + Q 被按下!");
            // 在此可以添加执行操作的代码
            //关闭复现线程
           repeatPanel.quitRepeat();

        }else if (e.getKeyCode() == NativeKeyEvent.VC_S && NativeKeyEvent.getModifiersText(e.getModifiers()).contains("Ctrl")) {//检测 ctrl + s
            log.info("快捷键 ctrl + s");
            //关闭复现线程
            repeatPanel.stopRepeat();
            //关闭点击频率线程
            clickFrequencyPanel.stopClick();

            //关闭执行次数
            Main.gestureWindow.stopRepeatNumPanel();
        }


    }


    @Override
    public void nativeKeyTyped(NativeKeyEvent nativeEvent) {
        NativeKeyListener.super.nativeKeyTyped(nativeEvent);
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent nativeEvent) {
        NativeKeyListener.super.nativeKeyReleased(nativeEvent);
    }
}
